import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "../utils/auth";

import { ElMessage } from 'element-plus'
import Login from "../views/User/Login.vue";
import Register from "../views/User/Register.vue";
import DataView from "../views/DataView/DataView.vue";
import Add from "../views/ProductList/Add.vue";
import ProductInfo from "../views/DataView/ProductInfo.vue";
import ProductList from "../views/ProductList/ProductList.vue";
import Edit from "../views/ProductList/Edit.vue";
import UserList from "../views/VipUser/UserList.vue";
import UserInfo from "../views/VipUser/UserInfo.vue";
import EditUser from "../views/VipUser/EditUser.vue";
import AddUser from "../views/VipUser/AddUser.vue";
import AdminInfo from "../views/User/AdminInfo.vue";
const routes = [{
    path: '/',
    name: 'Login',
    component: Login,
},
{path: '/register', name:'Register', component: Register},
{path: '/dataView', name:'DataView', component: DataView},
{path: '/add', name:'Add', component: Add},
{path: '/productInfo/:id', name:'ProductInfo', component: ProductInfo},
{path: '/productList', name:'ProductList', component: ProductList},
{path: '/productEdit/:id', name:'Edit', component: Edit},
{path: '/userList', name:'UserList', component: UserList},
{path: '/userInfo/:id', name:'UserInfo', component: UserInfo},
{path: '/editUser/:id', name:'EditUser', component: EditUser},
{path: '/addUser', name:'AddUser', component: AddUser},
{path: '/adminInfo/:id', name:'AdminInfo', component: AdminInfo},

]
const router = createRouter({
    history: createWebHistory(),
    routes,
  });
  router.beforeEach((to, from) => {
    if (to.path === "/"|| to.path === "/register") {
      return true;
    } else if (!getToken()) {
      ElMessage.error("请先登录");
      router.push("/");
      return false;
    } else {
      return true;
    }
  });
  export default router;